{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deploy Watson ML"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Deploys model to IBM Watson Machine Learning\n",
    "Currently only supports TensorFlow V2.4 SavedModel format. More to come..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install ibm-watson-machine-learning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ibm_watson_machine_learning import APIClient\n",
    "import json\n",
    "import logging\n",
    "import sys\n",
    "import time\n",
    "import re\n",
    "import shutil\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# IBM Cloud api_key https://cloud.ibm.com/iam/apikeys\n",
    "api_key = os.environ.get('api_key')\n",
    "\n",
    "# IBM Cloud deployment space ID https://dataplatform.cloud.ibm.com/ml-runtime/spaces\n",
    "space = os.environ.get('space')\n",
    "\n",
    "# IBM Cloud location (default: us-south)\n",
    "location = os.environ.get('location', 'us-south')\n",
    "\n",
    "# Model zip file name\n",
    "model_zip = os.environ.get('model_zip', 'model.zip')\n",
    "\n",
    "# IBM Cloud WML Deployment Name\n",
    "deployment_name = os.environ.get('deployment_name', 'Deployment Name')\n",
    "\n",
    "# IBM Cloud WML Model Name\n",
    "model_name = os.environ.get('model_name', 'Model Name')\n",
    "\n",
    "# temporal data storage for local execution\n",
    "data_dir = os.environ.get('data_dir', '../../data/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters = list(\n",
    "    map(lambda s: re.sub('$', '\"', s),\n",
    "        map(\n",
    "            lambda s: s.replace('=', '=\"'),\n",
    "            filter(\n",
    "                lambda s: s.find('=') > -1 and bool(re.match(r'[A-Za-z0-9_]*=[.\\/A-Za-z0-9]*', s)),\n",
    "                sys.argv\n",
    "            )\n",
    "    )))\n",
    "\n",
    "for parameter in parameters:\n",
    "    logging.warning('Parameter: ' + parameter)\n",
    "    exec(parameter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wml_credentials = {\n",
    "    \"apikey\": api_key,\n",
    "    \"url\": 'https://' + location + '.ml.cloud.ibm.com'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = APIClient(wml_credentials)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_folder = str(time.time())\n",
    "shutil.unpack_archive(data_dir + model_zip, extract_dir=data_dir + model_folder)\n",
    "data_dir_model_folder = data_dir + model_folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash -s \"$data_dir_model_folder\"\n",
    "cd $1\n",
    "tar -czvf ../model.tar.gz *\n",
    "cd .."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "o = client.software_specifications.get_uid_by_name('tensorflow_2.4-py3.7')\n",
    "software_spec_uid = o\n",
    "client.set.default_space(space)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_meta_props = {\n",
    "    client.repository.ModelMetaNames.NAME: deployment_name,\n",
    "    client.repository.ModelMetaNames.TYPE: \"tensorflow_2.4\",\n",
    "    client.repository.ModelMetaNames.SOFTWARE_SPEC_UID: software_spec_uid\n",
    "}\n",
    "\n",
    "published_model = client.repository.store_model(\n",
    "    model='model.tar.gz',\n",
    "    meta_props=model_meta_props\n",
    ")\n",
    "\n",
    "model_uid = client.repository.get_model_uid(published_model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_details = client.repository.get_details(model_uid)\n",
    "print(json.dumps(model_details, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client.repository.list_models(limit=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment = client.deployments.create(\n",
    "    model_uid,\n",
    "    meta_props={\n",
    "        client.deployments.ConfigurationMetaNames.NAME: model_name,\n",
    "        client.deployments.ConfigurationMetaNames.ONLINE: {}\n",
    "    }\n",
    ")\n",
    "\n",
    "deployment_uid = client.deployments.get_uid(deployment)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment_uid"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
